package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.TimerTask;

// 代码滚动状态
public class CodeScrollState implements GameState {
    private final CodeRebirthGame game;
    private Timer codeTimer, dialogTimer;
    private int yPos = 0;
    private String[] codeLines = {
            "public class Main {",
            "    public static void main(String[] args) {",
            "        System.out.println(\"Hello World\");",
            "        // 更多代码...",
            "        for (int i = 0; i < 10; i++) {",
            "            System.out.println(i);",
            "        }",
            "    }",
            "}"
    };

    public CodeScrollState(CodeRebirthGame game) {
        this.game = game;
    }

    @Override
    public void enter() {
        game.setLayout(new BorderLayout());

        // 代码滚动效果
        codeTimer = new Timer();
        codeTimer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                yPos -= 2;
                if (yPos < -game.getHeight()) {
                    yPos = 0;
                }
                game.repaint();
            }
        }, 0, 30);

        // 5秒后显示对话框
        dialogTimer = new Timer();
        dialogTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                showDialog();
            }
        }, 5000);
    }

    // 修改showDialog()方法：
    private void showDialog() {
        JPanel dialogPanel = new JPanel(new BorderLayout());
        dialogPanel.setBackground(new Color(0, 0, 255, 180));

        JButton saveBtn = new JButton("存档");
        saveBtn.addActionListener(e -> {
            game.getPlayerData().saveGame();
            game.changeState(new ConcreteRepairState(game));
        });

        JButton continueBtn = new JButton("继续游戏");
        continueBtn.addActionListener(e -> game.changeState(new ConcreteRepairState(game)));

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(saveBtn);
        buttonPanel.add(continueBtn);

        dialogPanel.add(new JLabel("选择你的操作:", JLabel.CENTER), BorderLayout.NORTH);
        dialogPanel.add(buttonPanel, BorderLayout.CENTER);

        game.add(dialogPanel, BorderLayout.CENTER);
        game.revalidate();
    }

    private void styleButton(JButton button) {
        button.setFont(game.getResourceManager().getFont("main", 16f));
        button.setBackground(new Color(70, 130, 180));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
    }

    @Override
    public void render(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, game.getWidth(), game.getHeight());

        // 绘制绿色代码
        g.setColor(new Color(0, 255, 0));
        g.setFont(game.getResourceManager().getFont("code", 16f));

        int y = yPos;
        for (String line : codeLines) {
            g.drawString(line, 50, y);
            y += 30;
        }

        // 绘制更多代码行
        for (int i = 0; i < 20; i++) {
            g.drawString("// 自动生成的代码行 " + i, 50, y);
            y += 30;
        }
    }

    @Override public void update() {}

    @Override
    public void exit() {
        if (codeTimer != null) codeTimer.cancel();
        if (dialogTimer != null) dialogTimer.cancel();
        game.getContentPane().removeAll();
    }

    @Override public void handleInput(KeyEvent e) {}
}
